using System;
using System.Reflection;

namespace kbd.library.helper
{
    public static class PerformanceLogHelper
    {
        /// <summary>
        /// Logs a generic value metric
        /// </summary>
        /// <param name="value">Value to log</param>
        /// <param name="metricName">Name of the metric to use</param>
        /// <param name="recordDate">Date of the hit</param>
        /// <param name="assembly">Assembly that is logging</param>
        public static void Log(double value, string metricName, DateTime? recordDate = null, Assembly assembly = null)
        {
            if (assembly == null) assembly = Assembly.GetCallingAssembly();
            LogPerformance(value, metricName, Units.Default, recordDate, assembly);
        }

        /// <summary>
        /// Logs a "Number of Hits" metric
        /// </summary>
        /// <param name="metricName">Name of the metric to use</param>
        /// <param name="recordDate">Date of the hit</param>
        /// <param name="assembly">Assembly that is logging</param>
        public static void LogHit(string metricName, DateTime? recordDate = null, Assembly assembly = null)
        {
            if (assembly == null) assembly = Assembly.GetCallingAssembly();
            LogPerformance(1, metricName, Units.Hits, recordDate, assembly);
        }

        /// <summary>
        /// Logs an entry into the performance log
        /// </summary>
        /// <param name="value">Value to log</param>
        /// <param name="metricName">Metric of the value to log</param>
        /// <param name="unit">Unit of the value to log</param>
        /// <param name="recordDate">Date of the value</param>
        /// <param name="assembly">Assembly that is logging</param>
        public static void LogPerformance(double value, string metricName, Units unit = Units.Millisecond, DateTime? recordDate = null, Assembly assembly = null)
        {
            // Getting calling assembly if none was provided
            if (assembly == null) assembly = Assembly.GetCallingAssembly();

            // Getting assembly information
            var assemblyName = assembly.GetName();
            var assemblyVersion = assemblyName.Version.ToString();

            try
            {
                // Checking if the source exists
                //logger.CheckSource(assemblyName.Name, assemblyVersion);

                logger.Log(LogLevels.Information, String.Format("PERFORMANCE {0} : {1} {2}", metricName, value, unit ));
            }
            catch (Exception ex)
            {
                logger.Log(LogLevels.Critical, "LogPerformance", String.Format(" {0} : {1} {2} : ", metricName, value, unit), ex);
            }
        }

        /// <summary>
        /// Checks if a metric exists and creates it if needed
        /// </summary>
        /// <param name="metricName">Name of the metric to check</param>
        /// <param name="unit">Unit of the metric</param>
        public static void CheckMetric(string metricName, Units unit)
        {
            //using (var context = new HubLogging())
            //{
            //    // Trying to get the metric
            //    var metric = context.Metrics.FirstOrDefault(m => m.MetricName == metricName && m.UnitId == (int)unit);

            //    // Checking if the metric exists
            //    if (metric == null)
            //    {
            //        metric = new Metric()
            //        {
            //            MetricName = metricName,
            //            UnitId = (int)unit
            //        };

            //        context.Metrics.ApplyLoggingChanges(metric);
            //        context.SaveChanges();
            //    }
            //}
        }

    }
}